﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using DAL;
using BLL;
using System.Data;

namespace Web.Admin
{
    public partial class NhomSanpham : System.Web.UI.Page
    {
        public static bool insert;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadDSLoai();
            }
        }

        public void loadDSLoai()
        {
            GridNhomSP.DataSource = BLL.PhanloaiBLL.Phanloai_GetByAll();
            GridNhomSP.DataBind();
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            panelchitiet.Enabled = true;
            txtMa.Enabled = true;
            ControlClass.ResetControlValues(panelchitiet);
            insert = true;
        }

        protected void GridNhomSP_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            PhanloaiInfo obj = new PhanloaiInfo();
            string id = e.CommandArgument.ToString();
            if (e.CommandName == "Edit")
            {
                insert = false;
                DataTable dt = PhanloaiBLL.Phanloai_GetById(id);
                txtMa.Text = dt.Rows[0]["Ma_Phan_loai"].ToString();
                txtTen.Text = dt.Rows[0]["Ten_Phan_loai"].ToString();
                panelchitiet.Enabled = true;
                txtMa.Enabled = false;
            }
            else
            {
                PhanloaiBLL.Phanloai_Delete(id);
                loadDSLoai();
            }
        }

        protected void imgLuu_Click(object sender, ImageClickEventArgs e)
        {
            #region[Kiemtra]
            if (txtMa.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Bạn chưa nhập Mã phân loại");
                txtMa.Focus();
                return;
            }
            if (txtTen.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Bạn chưa nhập Tên phân loại");
                txtTen.Focus();
                return;
            }
            #endregion
            var obj = new PhanloaiInfo();
            obj.Ma_Phan_loai = txtMa.Text;
            obj.Ten_Phan_loai = txtTen.Text;
            if (insert)
            {
                PhanloaiBLL.Phanloai_Insert(obj);
            }
            else
            {
                PhanloaiBLL.Phanloai_Update(obj);
            }
            ControlClass.ResetControlValues(panelchitiet);
            loadDSLoai();
            panelchitiet.Enabled = false;
        }

        protected void imgBack_Click(object sender, ImageClickEventArgs e)
        {
            ControlClass.ResetControlValues(panelchitiet);
            panelchitiet.Enabled = false;
        }

        protected void GridNhomSP_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            GridNhomSP.CurrentPageIndex = e.NewPageIndex;
            loadDSLoai();
        }
    }
}